package com.wei.chatroom.controller;

import com.wei.chatroom.model.Friend;
import com.wei.chatroom.model.FriendMapper;
import com.wei.chatroom.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: chatroom
 * @package: com.wei.chatroom.controller
 * @className: FriendController
 * @author: weiJiang
 * @description: 获取某个用户的朋友列表接口类
 * @date: 2025/9/22 下午3:18
 * @version: 1.0
 */

@RestController
public class FriendController {

    @Autowired
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    public Object getFriendList(HttpServletRequest req){
        //从会话获取当前用户，才能进行查询
        HttpSession session=req.getSession(false);
        if(session==null){
            System.out.println("friend类中没有获取到session");
            return new ArrayList<Friend>();
        }

        User user=(User) session.getAttribute("user");

        if(user==null){
            System.out.println("friend类中没有获取到好友列表");
            return new ArrayList<Friend>();
        }

        List<Friend>friendList=friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }
}
